package com.xu.provider.service.Impl;

import com.alibaba.fastjson.JSON;
import com.xu.common.config.RedisKeyConfig;
import com.xu.common.config.SystemConfig;
import com.xu.common.util.DateUtil;
import com.xu.common.util.HttpUtil;
import com.xu.common.util.RedissionUtil;
import com.xu.common.vo.R;
import com.xu.entity.dto.JdWeatherDto;
import com.xu.entity.dto.WeatherDto;
import com.xu.entity.pojo.Weather;
import com.xu.provider.dao.WeatherProviderDao;
import com.xu.provider.service.WeatherProviderSaervice;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 徐老板
 * @date 2020/4/1   19:21
 */
@Service
public class WeatherProviderSaerviceImpl implements WeatherProviderSaervice {

    @Resource
    private WeatherProviderDao weatherDao;

    @Override
    public R queryToday(String city) {
        System.out.println("===========================开始查询==================================");

        if (RedissionUtil.checkHash(RedisKeyConfig.KEY_WEATHER_TODAY, city)){
            String hashVal = RedissionUtil.getHashVal(RedisKeyConfig.KEY_WEATHER_TODAY, city);
            //看意思是把字符串转换为实体类
            return R.success(JSON.parseObject(hashVal, Weather.class));
        }else {
            //redis中查询没有 就去数据库中查
            Weather weather = weatherDao.getByCityAndCdate(city, new Date());
            if (weather!=null){
                //这个城市的 今日的天气信息  存在
                //同步数据到Redis 需要校验是不是第一次 第一次设置有效期
                if(!RedissionUtil.checkKey(RedisKeyConfig.KEY_WEATHER_TODAY)){
                    //创建并设置有效期  当日有效  24-now
                    RedissionUtil.saveHash(RedisKeyConfig.KEY_WEATHER_TODAY,
                            city, JSON.toJSONString(weather), DateUtil.getSeconds());
                }else {
                    RedissionUtil.saveHash(RedisKeyConfig.KEY_WEATHER_TODAY, city, JSON.toJSONString(weather));
                }
                return R.success(weather);
            }else {
                //数据库中的数据也不存在 这个时候就需要去第三方接口去查询
                String json = HttpUtil.getJson(SystemConfig.WEATHER_URL + city);

                System.out.println("=============================从第三方接口去查询==================================");
                System.out.println("=============================前端传递的城市名称是========"+city);
                System.out.println("=============================发送的字符串是==============="+json);

                if (json!=null){
                    JdWeatherDto jdWeatherDto = JSON.parseObject(json, JdWeatherDto.class);
                    System.out.println("=============================封装后的实体类是==============="+jdWeatherDto);
                    //按前端的数据格式封装到这个java类以后就可以通过调用java类的属性对前端传过来的状态码进行判断了
                    if (jdWeatherDto.getCode()==10000&&jdWeatherDto.getResult().getStatus()==0){
                        //这个时候证明前端传过来的数据ok 这个时候就可以把前端没有的数据补全传给后端数据库了
                        //由于我们的需要的数据都封装在前端传过来的数据的result属性中
                        // 所以我们要先把这我们需要的数据取到
                        WeatherDto weatherDto = jdWeatherDto.getResult().getResult();

                        System.out.println("=========================查询的结果是=================="+weatherDto);

                        //接下来开始填充数据
                        Weather needWeather =new Weather();

                        needWeather.setCdate(weatherDto.getDate());
                        needWeather.setCity(weatherDto.getCity());
                        needWeather.setCitycode(weatherDto.getCitycode());
                        needWeather.setTemphigh(weatherDto.getTemphigh());
                        needWeather.setTemplow(weatherDto.getTemplow());
                        needWeather.setWeather(weatherDto.getWeather());
                        needWeather.setWeek(weatherDto.getWeek());
                        needWeather.setWinddirect(weatherDto.getWinddirect());
                        needWeather.setWindpower(weatherDto.getWindpower());
                        needWeather.setCtime(new Date());
                        //存储到Mysql
                        weatherDao.save(needWeather);

                        if(!RedissionUtil.checkKey(RedisKeyConfig.KEY_WEATHER_TODAY)){
                            //创建并设置有效期  当日有效  24-now
                            RedissionUtil.saveHash(RedisKeyConfig.KEY_WEATHER_TODAY,
                                    city, JSON.toJSONString(needWeather),DateUtil.getSeconds());
                        }else {
                            RedissionUtil.saveHash(RedisKeyConfig.KEY_WEATHER_TODAY, city, JSON.toJSONString(needWeather));
                        }
                        //作为逻辑处理后再把需要的数据返回给前台 所以昨天的感悟是业务逻辑存取数据库发生在
                        //接收参数和相应数据的中间层
                        return R.success(needWeather);

                    }
                }
            }
        }
        return R.error("城市异常");
    }

    @Override
    public R queryAll(String city) {

        return R.success(weatherDao.queryCity(city));
    }

    @Override
    public R queryAll() {
        //这个findall是datajpa里面提供的方法
        return R.success(weatherDao.findAll());
    }
}
